#include "FileExplorerSettings.h"
#include <sdk.h>
#include <wx/arrimpl.cpp>

WX_DEFINE_OBJARRAY( FavoriteDirs );
BEGIN_EVENT_TABLE( FileBrowserSettings, wxDialog )
  EVT_BUTTON( XRCID( "idnew" ), FileBrowserSettings::New )
  EVT_BUTTON( XRCID( "iddelete" ), FileBrowserSettings::Delete )
  EVT_BUTTON( XRCID( "idup" ), FileBrowserSettings::OnUp )
  EVT_BUTTON( XRCID( "iddown" ), FileBrowserSettings::OnDown )
  EVT_BUTTON( XRCID( "idbrowsepath" ), FileBrowserSettings::OnBrowse )
  EVT_BUTTON( wxID_OK, FileBrowserSettings::OnOk )
  EVT_LISTBOX( XRCID( "idfavlist" ), FileBrowserSettings::ChangeSelection )
  EVT_TEXT( XRCID( "idalias" ), FileBrowserSettings::NameChange )
END_EVENT_TABLE()

FileBrowserSettings::FileBrowserSettings( const FavoriteDirs &favdirs, wxWindow* parent, int, wxPoint, wxSize, int ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/fileexplorersettings.xrc" ) ;
  xml_res->Load( resPath );
  if( !xml_res->LoadDialog( this, parent, _T( "FileBrowserSettings" ) ) ) {
    return;
  }
  idfavlist = XRCCTRL( *this, "idfavlist", wxListBox );
  idalias = XRCCTRL( *this, "idalias", wxTextCtrl );
  idpath = XRCCTRL( *this, "idpath", wxTextCtrl );
  m_favdirs = favdirs;
  for( size_t i = 0; i < favdirs.GetCount(); i++ ) {
    idfavlist->Append( favdirs[i].alias );
  }
  m_selected = -1;
  if( m_favdirs.GetCount() > 0 ) {
    m_selected = 0;
    idalias->SetValue( m_favdirs[0].alias );
    idpath->SetValue( m_favdirs[0].path );
  }
  idfavlist->SetSelection( m_selected );
  SetSize( wxSize( 500, 500 ) );
}

void FileBrowserSettings::New( wxCommandEvent & ) {
  FavoriteDir f;
  f.alias = _T( "New Path" );
  f.path = _T( "" );
  m_favdirs.Add( FavoriteDir() );
  idfavlist->Append( f.alias );
  m_selected = idfavlist->GetCount() - 1;
  idfavlist->SetSelection( m_selected );
  idalias->SetValue( f.alias );
  idpath->SetValue( f.path );
}

void FileBrowserSettings::Delete( wxCommandEvent & ) {
  int i = idfavlist->GetSelection();
  if( i < 0 ) {
    return;
  }
  m_favdirs.RemoveAt( i );
  idfavlist->Delete( i );
  if( ( unsigned int )i >= idfavlist->GetCount() ) {
    i--;
  }
  if( i >= 0 ) {
    idfavlist->SetSelection( i );
    m_selected = i;
    idalias->SetValue( m_favdirs[i].alias );
    idpath->SetValue( m_favdirs[i].path );
  } else {
    idfavlist->SetSelection( -1 );
    m_selected = -1;
  }
}

void FileBrowserSettings::OnUp( wxCommandEvent & ) {
  int i = idfavlist->GetSelection();
  if( i <= 0 ) {
    return;
  }
  m_favdirs[i].alias = idalias->GetValue();
  m_favdirs[i].path = idpath->GetValue();
  FavoriteDir fswap;
  fswap = m_favdirs[i];
  m_favdirs[i] = m_favdirs[i - 1];
  m_favdirs[i - 1] = fswap;
  idfavlist->SetString( i - 1, m_favdirs[i - 1].alias );
  idfavlist->SetString( i, m_favdirs[i].alias );
  idfavlist->SetSelection( i - 1 );
  m_selected = i - 1;
}

void FileBrowserSettings::OnDown( wxCommandEvent & ) {
  int i = idfavlist->GetSelection();
  if( i < 0 ) {
    return;
  }
  if( ( unsigned int )i >= idfavlist->GetCount() - 1 ) {
    return;
  }
  m_favdirs[i].alias = idalias->GetValue();
  m_favdirs[i].path = idpath->GetValue();
  FavoriteDir fswap;
  fswap = m_favdirs[i];
  m_favdirs[i] = m_favdirs[i + 1];
  m_favdirs[i + 1] = fswap;
  idfavlist->SetString( i + 1, m_favdirs[i + 1].alias );
  idfavlist->SetString( i, m_favdirs[i].alias );
  idfavlist->SetSelection( i + 1 );
  m_selected = i + 1;
}

void FileBrowserSettings::ChangeSelection( wxCommandEvent & ) {
  int i = idfavlist->GetSelection();
  if( i < 0 ) {
    return;
  }
  if( ( unsigned int )i >= idfavlist->GetCount() ) {
    return;
  }
  m_favdirs[m_selected].alias = idalias->GetValue();
  m_favdirs[m_selected].path = idpath->GetValue();
  idfavlist->SetString( i - 1, m_favdirs[i - 1].alias );
  idfavlist->SetString( i, m_favdirs[i].alias );
  m_selected = i;
  idalias->SetValue( m_favdirs[i].alias );
  idpath->SetValue( m_favdirs[i].path );
}

void FileBrowserSettings::NameChange( wxCommandEvent & ) {
  if( m_selected < 0 ) {
    return;
  }
  if( ( unsigned int )m_selected >= idfavlist->GetCount() ) {
    return;
  }
  idfavlist->SetString( m_selected, idalias->GetValue() );
}

void FileBrowserSettings::OnOk( wxCommandEvent & ) {
  m_favdirs[m_selected].alias = idalias->GetValue();
  m_favdirs[m_selected].path = idpath->GetValue();
  EndModal( wxID_OK );
}

void FileBrowserSettings::OnBrowse( wxCommandEvent & ) {
  wxDirDialog *dd = new wxDirDialog( NULL, _T( "Choose a Directory" ) );
  dd->SetPath( idpath->GetValue() );
  if( dd->ShowModal() == wxID_OK ) {
    idpath->SetValue( dd->GetPath() );
  }
  delete dd;
}
